package activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.example.administrator.ci_edit.R;

import java.util.ArrayList;
import java.util.List;

import adapter.FragAdapter;
import fragment.ShequFragment;
import fragment.VipFragment;
import fragment.WodeFragment;
import fragment.XieciFragment;

/**
 * Created by hello world on 2017/7/21.
 */

public class SupperActivity extends FragmentActivity implements android.view.View.OnClickListener{
    private ViewPager mViewPager;// 用来放置界面切换
    //private PagerAdapter mPagerAdapter;// 初始化View适配器
    //private List<View> mViews = new ArrayList<View>();// 用来存放Tab01-04
    // 四个Tab，每个Tab包含一个按钮
    private LinearLayout mTabLogin;
    private LinearLayout mTabXieci;
    private LinearLayout mTabShequ;
    private LinearLayout mTabVip;
    // 四个按钮
    private ImageButton mLoginImg;
    private ImageButton mXieciImg;
    private ImageButton mShequImg;
    private ImageButton mVipImg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//把标题去掉了
        setContentView(R.layout.activity_supper);
        initView();
        initViewPage();
        initEvent();
    }
    /**
     * 初始化设置
     */
    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.id_viewpage);
        // 初始化四个LinearLayout
        mTabLogin = (LinearLayout) findViewById(R.id.id_tab_login);
        mTabXieci = (LinearLayout) findViewById(R.id.id_tab_xieci);
        mTabShequ = (LinearLayout) findViewById(R.id.id_tab_shequ);
        mTabVip = (LinearLayout) findViewById(R.id.id_tab_vip);
        // 初始化四个按钮
        mLoginImg = (ImageButton) findViewById(R.id.id_tab_login_img);
        mXieciImg = (ImageButton) findViewById(R.id.id_tab_xieci_img);
        mShequImg = (ImageButton) findViewById(R.id.id_tab_shequ_img);
        mVipImg = (ImageButton) findViewById(R.id.id_tab_vip_img);
    }

    /**
     * 初始化ViewPage
     */
    private void initViewPage() {

        // 初始化四个布局
   /*     LayoutInflater mLayoutInflater = LayoutInflater.from(this);
        View tab01 = mLayoutInflater.inflate(R.layout.login, null);
        View tab02 = mLayoutInflater.inflate(R.layout.activity_main, null);
        View tab03 = mLayoutInflater.inflate(R.layout.fc_template, null);
        View tab04 = mLayoutInflater.inflate(R.layout.activity_dashang, null);

        mViews.add(tab01);
        mViews.add(tab02);
        mViews.add(tab03);
        mViews.add(tab04);*/

        List<Fragment> fragments=new ArrayList<Fragment>();
        fragments.add(new XieciFragment());
        fragments.add(new ShequFragment());
        fragments.add(new VipFragment());
        fragments.add(new WodeFragment());

        FragAdapter fragadapter = new FragAdapter(getSupportFragmentManager(), fragments);
        mViewPager.setAdapter(fragadapter);

       // 适配器初始化并设置
       /*  mPagerAdapter = new PagerAdapter() {

            @Override
            public void destroyItem(ViewGroup container, int position,
                                    Object object) {
                container.removeView(mViews.get(position));

            }
            //现在的这个
            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                View view = mViews.get(position);
                container.addView(view);
                return view;
            }

            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {

                return arg0 == arg1;
            }

            @Override
            public int getCount() {

                return mViews.size();
            }
        };
        mViewPager.setAdapter(mPagerAdapter);*/
    }

    private void initEvent() {
        mTabXieci.setOnClickListener(this);
        mTabShequ.setOnClickListener(this);
        mTabVip.setOnClickListener(this);
        mTabLogin.setOnClickListener(this);



        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            /**
             *ViewPage左右滑动时
             */
            @Override
            public void onPageSelected(int arg0) {
                int currentItem = mViewPager.getCurrentItem();
                switch (currentItem) {
                    case 0:
                        resetImg();
                        mXieciImg.setImageResource(R.drawable.xieci_pressed);
                        break;
                    case 1:
                        resetImg();
                        mShequImg.setImageResource(R.drawable.shequ_pressed);
                        break;
                    case 2:
                        resetImg();
                        mVipImg.setImageResource(R.drawable.vip_pressed);
                        break;
                    case 3:
                        resetImg();
                        mShequImg.setImageResource(R.drawable.vip_pressed);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }






    /**
     * 判断哪个要显示，及设置按钮图片
     */
    @Override
    public void onClick(View arg0) {

        switch (arg0.getId()) {
            case R.id.id_tab_login:
                mViewPager.setCurrentItem(3);
                resetImg();
                mLoginImg.setImageResource(R.drawable.login_pressed);
                break;
            case R.id.id_tab_xieci:
                mViewPager.setCurrentItem(0);
                resetImg();
                mXieciImg.setImageResource(R.drawable.xieci_pressed);
                break;
            case R.id.id_tab_shequ:
                mViewPager.setCurrentItem(1);
                resetImg();
                mShequImg.setImageResource(R.drawable.shequ_pressed);
                break;
            case R.id.id_tab_vip:
                mViewPager.setCurrentItem(2);
                resetImg();
                mVipImg.setImageResource(R.drawable.vip_pressed);
                break;
            default:
                break;
        }
    }

    /**
     * 把所有图片变成正常的那种
     */
    private void resetImg() {
        mLoginImg.setImageResource(R.drawable.login);
        mXieciImg.setImageResource(R.drawable.xieci);
        mShequImg.setImageResource(R.drawable.shequ);
        mVipImg.setImageResource(R.drawable.vip);
    }

}
